﻿
@{
    ViewBag.Title = "待报价";
    Layout = "~/Views/Shared/_LayoutEasyUI.cshtml";
}

<table id="grid-list"> </table>

@section scripts{
    <script>
        var CurrentFIndex;
        $(function () {
            Grid.LoadData();

        });

        var Grid = {
            LoadData: function () {
                var url = "/Order/QuoteData";
                Grid.list = $("#grid-list").datagrid({
                    url: url,
                    title: '待报价订单列表',
                    fit: true,
                    fitColumns: true,
                    singleSelect: false,
                    rownumbers: true,
                    nowrap: false,
                    striped: true, //隔行变色
                    toolbar: [{
                        iconCls: 'icon-print',
                        text: '提交报价',
                        handler: function () {
                            layer.confirm('确认提交报价？', function (index) {
                                if (endEditing()) {
                                    save();
                                    layer.close(index);
                                }
                            });

                        }
                    }],
                    columns: [[
                        { field: 'ck', checkbox: true, align: 'left', width: 50 },
                        { field: 'FOrderNo', title: '订单编号', width: 100, align: 'center' },
                        { field: 'FStoreName', title: '门店', width: 100, align: 'center' },
                        { field: 'FAddress', title: '联系地址', width: 100, align: 'center' },
                        { field: 'FIndex', title: '行号', width: 100, align: 'center' },
                        { field: 'FItemName', title: '产品名称', width: 100, align: 'center' },
                        { field: 'FGgModel', title: '规格型号', width: 100, align: 'center' },
                        { field: 'FQty', title: '数量', width: 100, align: 'center' },
                        { field: 'FWeight', title: '重量', width: 100, align: 'center' },
                        { field: 'FVolume', title: '体积', width: 100, align: 'center' },
                        { field: 'FBjPrice', title: '保价金额', width: 100, align: 'center' },
                        { field: 'FDt', title: '有无电梯', width: 100, align: 'center' },
                        {
                            field: 'TranspPrice1', title: '报价(入户)', width: 80, align: 'center',
                            editor: { type: 'numberbox', options: { precision: 2 } },
                            formatter: function (value, row, index) {
                                if (value == 0) {
                                    return '';
                                } else {
                                    return value;
                                }
                            }
                        },
                        {
                            field: 'TranspPrice2', title: '报价(不入户)', width: 80, align: 'center', editor: {
                                type: 'numberbox', options: { precision: 2 },
                                formatter: function (value, row, index) {
                                    if (value == 0) {
                                        return '';
                                    } else {
                                        return value;
                                    }
                                }
                            }
                        }
                    ]],
                    //onLoadSuccess: onLoadSuccess,
                    onClickCell: onClickCell,
                    onClickRow: function (index, row) {
                        CurrentFIndex = row.FIndex;
                        if (CurrentFIndex != undefined && CurrentFIndex > 1) {
                            $('#grid-list').datagrid('endEdit', editIndex);
                        }

                    }
                });
            },
            Refresh: function () {
                Grid.list.datagrid('reload');
            }
        }
        var editIndex = undefined;
        function endEditing() {
            if (editIndex == undefined) { return true }
            if ($('#grid-list').datagrid('validateRow', editIndex)) {
                $('#grid-list').datagrid('endEdit', editIndex);
                editIndex = undefined;
                return true;
            } else {
                return false;
            }
        }
        function onClickCell(index, field) {
            if (editIndex != index) {
                if (endEditing()) {
                    console.log("onClickCell:" + CurrentFIndex);
                    //todo：获取行信息的行号

                    $('#grid-list').datagrid('selectRow', index)
                        .datagrid('beginEdit', index);
                    var ed = $('#grid-list').datagrid('getEditor', { index: index, field: field });
                    if (ed) {
                        ($(ed.target).data('textbox') ? $(ed.target).textbox('textbox') : $(ed.target)).focus();
                    }
                    editIndex = index;
                } else {
                    setTimeout(function () {
                        $('#grid-list').datagrid('selectRow', editIndex);
                    }, 0);
                }
            }
        }
        function save() {
            var rows = $('#grid-list').datagrid('getChanges');
            //todo: 报价为0 bug
            if (rows.length > 0) {
                $.ajax({
                    url: '/Order/SupplierBj',
                    data: {
                        orders: rows//JSON.stringify(rows)
                    },
                    dataType: "json",
                    type: "post",
                    success: function (data) {
                        if (data.success) {
                            Grid.Refresh();
                        }
                    }
                });
            }
        }

        function onLoadSuccess(data) {
            var _orderid;
            var _index = -1;
            var _rowspan = 0;
            for (var i = 0; i < data.rows.length; i++) {
                if (_orderid != data.rows[i].FOrderId) {
                    //合并；
                    if (_rowspan > 1) {
                        $(this).datagrid('mergeCells', {
                            index: _index,
                            field: 'ck',
                            rowspan: _rowspan
                        }).datagrid('mergeCells', {
                            index: _index,
                            field: 'FOrderNo',
                            rowspan: _rowspan
                        }).datagrid('mergeCells', {
                            index: _index,
                            field: 'FStoreName',
                            rowspan: _rowspan
                        }).datagrid('mergeCells', {
                            index: _index,
                            field: 'FAddress',
                            rowspan: _rowspan
                        }).datagrid('mergeCells', {
                            index: _index,
                            field: 'TranspPrice1',
                            rowspan: _rowspan
                        }).datagrid('mergeCells', {
                            index: _index,
                            field: 'TranspPrice2',
                            rowspan: _rowspan
                        });
                    }
                    _orderid = data.rows[i].FOrderId;
                    _index = i;
                    _rowspan = 1;
                } else {
                    _rowspan = _rowspan + 1;
                }
            }
        }
    </script>
}